﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Dao;
using Web.Utils;

namespace Web.Page
{
    public partial class KC_Query : BasePage
    {
        protected KC_Dao dao = new KC_Dao();

        protected void Page_Load(object sender, EventArgs e)
        {
            base.Page_Load(sender, e);
            if (!IsPostBack)
            {
                string op = Request.QueryString["op"];
                this.hdOp.Value = op;
                string title = "{0}库存列表";
                this.lbTitle.Text = string.Format(title, op.Equals("01") ? "物料" : "产品");
                initValue();
                BindGridview();
            }
        }

        private void initValue()
        {
            if (this.hdOp.Value.Equals("01"))
            {
                InitWL();
            }
            else
            {
                InitCP();
            }

            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();
            this.ddlCWID.DataSource = lsCW;
            this.ddlCWID.DataValueField = "ID";
            this.ddlCWID.DataTextField = "BH";
            this.ddlCWID.DataBind();
            this.ddlCWID.Items.Insert(0, new ListItem("请选择", "0"));

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();
            this.ddlCKID.DataSource = lsCK;
            this.ddlCKID.DataValueField = "ID";
            this.ddlCKID.DataTextField = "MC";
            this.ddlCKID.DataBind();
            this.ddlCKID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        private void InitWL()
        {
            this.ddlWPID.Items.Clear();
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> lsZP = daoWL.QueryAll();
            this.ddlWPID.DataSource = lsZP;
            this.ddlWPID.DataValueField = "ID";
            this.ddlWPID.DataTextField = "MC";
            this.ddlWPID.DataBind();
            this.ddlWPID.Items.Insert(0, new ListItem("请选择", "0"));
        }


        private void InitCP()
        {
            this.ddlWPID.Items.Clear();
            ZL_CP_Dao daoCP = new ZL_CP_Dao();
            List<ZL_CP> lsZP = daoCP.QueryAll();
            this.ddlWPID.DataSource = lsZP;
            this.ddlWPID.DataValueField = "ID";
            this.ddlWPID.DataTextField = "BH";
            this.ddlWPID.DataBind();
            this.ddlWPID.Items.Insert(0, new ListItem("请选择", "0"));
        }

        protected void GridView1_PageIndexChanged(object sender, EventArgs e)
        {
            BindGridview();
        }

        protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            GridView1.PageIndex = e.NewPageIndex;
            BindGridview();
            SavePageIndex(e.NewPageIndex);
        }

        private void BindGridview()
        {
            ZL_WL_Dao daoWL = new ZL_WL_Dao();
            List<ZL_WL> lsWL = daoWL.QueryAll();

            ZL_CP_Dao daoZLCP = new ZL_CP_Dao();
            List<ZL_CP> lsCP = daoZLCP.QueryAll();

            // 仓位
            ZL_CW_Dao daoCW = new ZL_CW_Dao();
            List<ZL_CW> lsCW = daoCW.QueryAll();

            // 仓库
            ZL_CK_Dao daoCK = new ZL_CK_Dao();
            List<ZL_CK> lsCK = daoCK.QueryAll();

            // 单位
            CS_JBDW_Dao daoDW = new CS_JBDW_Dao();
            List<CS_JBDW> lsDw = daoDW.Query4WL();

            GridView1.PageIndex = RestorePageIndex();

            string sql = string.Format("select value c from KC as c where c.WPLX='{0}' ", this.hdOp.Value);
            List<KC> listKC = query(); //  dao.QueryBySql(sql);
            List<KC_Ex> listEx = new List<KC_Ex>();

            KC_Ex ce;
            foreach (KC c in listKC)
            {
                ce = new KC_Ex();
                ce.ID = c.ID.ToString();
                ce.WPLX = getWPLX(c.WPLX);
                ce.SL = c.SL.ToString();
                ce.ZXDJ = c.ZXDJ.ToString();
                ce.PJDJ = c.PJDJ.ToString();

                if (this.hdOp.Value.Equals("01"))
                {
                    ZL_WL wl = lsWL.Find(delegate(ZL_WL p) { return p.ID == c.WPID; });
                    ce.WPID = wl == null ? "" : wl.MC;
                }
                else if (this.hdOp.Value.Equals("02"))
                {
                    ZL_CP cp = lsCP.Find(delegate(ZL_CP p) { return p.ID == c.WPID; });
                    ce.WPID = cp == null ? "" : cp.BH;
                }

                ZL_CW cw = lsCW.Find(delegate(ZL_CW p) { return p.ID == c.CWID; });
                ce.CWID = cw == null ? "" : cw.BH;
                ZL_CK ck = lsCK.Find(delegate(ZL_CK p) { return p.ID == c.CKID; });
                ce.CKID = ck == null ? "" : ck.MC;
                CS_JBDW dw = lsDw.Find(delegate(CS_JBDW p) { return p.BH == c.DW; });
                ce.DW = dw == null ? "" : dw.MC;

                listEx.Add(ce);
            }


            this.GridView1.DataSource = listEx;
            this.GridView1.DataBind();
        }

        private string getWPLX(string key)
        {
            string name = "";
            string s = key.Trim();
            switch (s)
            {
                case "01":
                    name = "物料";
                    break;
                case "02":
                    name = "产品";
                    break;
                default:
                    name = "";
                    break;
            }
            return name;

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            //if (e.Row.RowType == DataControlRowType.DataRow)
            //{
            //    ((LinkButton)(e.Row.Cells[8].Controls[0])).Attributes.Add("onclick ", "return   confirm( '确定删除？ ') ");
            //}
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string id = GridView1.DataKeys[e.RowIndex].Value.ToString();
            int result = dao.Delete(id);
            if (result == 1)
            {
                lbResult.Text = "删除成功";
                BindGridview();
            }
        }

        protected class KC_Ex
        {
            public string ID { get; set; }
            public string WPID { get; set; }
            public string WPLX { get; set; }
            public string CKID { get; set; }
            public string CWID { get; set; }
            public string SL { get; set; }
            public string ZXDJ { get; set; }
            public string DW { get; set; }
            public string PJDJ { get; set; }
        }

        protected void btnAdd_Click(object sender, ImageClickEventArgs e)
        {
            if (hdOp.Value == "01")
                Response.Redirect("~/Page/WLKC_Add.aspx?op=" + hdOp.Value);
            else
                Response.Redirect("~/Page/KC_Add.aspx?op=" + hdOp.Value);
        }

        protected void btnQuery_Click(object sender, ImageClickEventArgs e)
        {
            BindGridview();
        }

        protected List<KC> query()
        {
            string lx = this.hdOp.Value;
            string wpid = this.ddlWPID.SelectedValue;
            string ckid = this.ddlCKID.SelectedValue;
            string cwid = this.ddlCWID.SelectedValue;

            return dao.QueryBySql(lx, wpid, ckid, cwid);
        }
    }
}